package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.dao.LogUserOperationMapper;
import com.hq.ims.data.entity.LogUserOperation;
import com.hq.ims.data.vo.LogUserOperationMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 * @Detail 用户操作登陆服务实现类
 */
@Service
public class BaseLogUserOperationService extends BaseService<LogUserOperationMapper, LogUserOperation> {

    public List<LogUserOperation> getPage(
            RequestPage page,
            LogUserOperationMapstruct.PageSearchVo searchVo
    ) {
        LambdaQueryChainWrapper<LogUserOperation> lq = this.lambdaQuery();
        lq.like(StringUtils.isNotEmpty(searchVo.getUserAccount()),LogUserOperation::getUserAccount,searchVo.getUserAccount());
        lq.ge(searchVo.getBeginTime() != null,LogUserOperation::getCreateDateTime,searchVo.getBeginTime());
        lq.ge(searchVo.getEndTime() != null,LogUserOperation::getCreateDateTime,searchVo.getEndTime());
        lq.orderByDesc(LogUserOperation::getCreateDateTime);
        lq.page(page);

        List<LogUserOperation> dataList = page.getRecords();
        return dataList;
    }

    public Boolean clearLogByTime(LogUserOperationMapstruct.ClearLogVo clearLogVo) {
        //通过日志id+时间区间清除日志
        return this.lambdaUpdate()
                .ge(clearLogVo.getBeginTime() != null, LogUserOperation::getCreateDateTime, clearLogVo.getBeginTime())
                .le(clearLogVo.getEndTime() != null, LogUserOperation::getCreateDateTime, clearLogVo.getEndTime())
                .remove();
    }
}
